package user.lan.com.usercenter;

import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.query.In;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import user.lan.com.entity.User;
import user.lan.com.inherit.ParentActivity;
import user.lan.com.usercenter.R;
import user.lan.com.util.DBHelper;

public class MainActivity extends ParentActivity {
    DBHelper mDbHelper;
    Dao<User, Integer> mUser;
    @BindView(R.id.et_username)
    EditText edt_name;
    @BindView(R.id.et_userpassd)
    EditText edt_pwd;
    @BindView(R.id.btn_register)
    Button btn_register;
    @OnClick(R.id.btn_register)
    void register(){

        testDao();
    }
    @OnClick(R.id.btn_login)
    void login(){
        try {
            List<User> user=mUser.queryForAll();
            for (int i = 0; i <user.size() ; i++) {
                Log.e("name",user.get(i).getName());
                if (edt_name.getText().toString().equals(user.get(i).getName())
                        &&edt_pwd.getText().toString().equals(user.get(i).getPwd())){
                    Intent intent =new Intent(mContext,Main2Activity.class);
                    intent.putExtra("url",user.get(i).getUrl());
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext,"密码错误",Toast.LENGTH_SHORT);
                }
            }
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected int getResourcesId() {
        return R.layout.activity_main;
    }

    @Override
    protected boolean isFullScreen() {
        return false;
    }

    @Override
    protected void setFullScreen() {

    }
    @Override
    protected void init(Bundle savedInstanceState) {
        mDbHelper = DBHelper.getInstance(this);
        try {
            mUser = mDbHelper.getUserDao();
        } catch (SQLException e) {
            Log.e(TAG, "constructor exception", e);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }

    }
    public void testDao(){
        User user =new User();
        user.setName("15619187872");
        user.setPwd("123");
        user.setId(0);
        user.setUrl("http://touxiang.qqzhi.com/uploads/2012-11/1111105304979.jpg");

        User user2 = new User();
        user2.setName("18710948468");
        user2.setPwd("111111");
        user2.setId(0);
        user2.setUrl("http://touxiang.qqzhi.com/uploads/2012-11/1111120502106.jpg");

        try {
            mUser.create(user);
            mUser.create(user2);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }

    }
}
